package com.component;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.component.tree.base.TreeDataProvider;
import org.operamasks.faces.component.tree.impl.UITree;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.CentralUnit;
import com.base.organization.entity.DepartmentalUnit;
import com.base.organization.entity.Employee;
import com.base.organization.entity.Employee.SEX;
import com.base.organization.service.IOrganizationService;
import com.base.security.entity.SecurityUser;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "component_personMultiSelectBean", scope = ManagedBeanScope.REQUEST)
public class PersonMultiSelectBean implements Serializable {
	protected IOrganizationService orgService = SpringBeanHolder
			.getBean("orgService");
	@Bind
	private UITree psm_selectTree;
	@Bind(id = "psm_selectTree")
	private TreeDataProvider treeData_psm_selectTree = new TreeDataProvider() {

		public Object[] getChildren(Object node) {
			// System.out.println("node value:" + node);
			if (node == null) {
				return new String[] { "机构" };
			}
			if ("机构".equals(node)) {
				return orgService.getTopCentralUnit().toArray();
			}
			if (node instanceof CentralUnit) {
				CentralUnit cu = (CentralUnit) node;
				List<CentralUnit> units = orgService.getDirectChildUnits(cu
						.getCentralUnitId());
				// 出机构的部门与机构的下级机构
				List<DepartmentalUnit> deps = orgService.getTopDeptOfUnit(cu
						.getCentralUnitId());
				int depSize = deps.size();
				int unitSize = units.size();
				Object[] result = new Object[depSize + unitSize];
				// 出机构部门
				for (int i = 0; i < depSize; i++) {
					result[i] = deps.get(i);
				}
				// 出子机构
				for (int n = 0; n < unitSize; n++) {
					result[n + depSize] = units.get(n);
				}
				return result;
			}
			if (node instanceof DepartmentalUnit) {
				DepartmentalUnit du = (DepartmentalUnit) node;
				List<DepartmentalUnit> temp = orgService.getDirectChildDepts(du
						.getDepartmentalUnitId());
				// 如果无子部门，出部门下面的员工
				if (temp.isEmpty()) {
					return orgService.getPersonsOfDept(
							du.getDepartmentalUnitId()).toArray();
				}
				List<SecurityUser> users = orgService.getPersonsOfDept(du
						.getDepartmentalUnitId());
				int userSize = users.size();
				int depSize = temp.size();
				Object[] result = new Object[depSize + userSize];
				// 如果该部门下面还有人，把人也需要出来。
				for (int i = 0; i < userSize; i++) {
					result[i] = users.get(i);
				}
				// 如果有子部门，出子部门
				for (int n = 0; n < depSize; n++) {
					result[n + userSize] = temp.get(n);
				}
				return result;
			}
			if (node instanceof SecurityUser)
				return null;
			return null;
		}

		public String getHref(Object nodeData) {
			return null;
		}

		public String getHrefTarget(Object nodeData) {
			if (nodeData instanceof SecurityUser)
				return ((SecurityUser) nodeData).getUserId().toString();
			return null;
		}

		public String getIcon(Object nodeData) {
			if (nodeData instanceof CentralUnit) {
				if (((CentralUnit) nodeData).getParentCentralUnitId() == null)
					return context.getExternalContext().getRequestContextPath()
							+ "/images/topOrg.gif";
				else
					return context.getExternalContext().getRequestContextPath()
							+ "/images/subOrg.gif";
			}
			if (nodeData instanceof DepartmentalUnit) {
				if (((DepartmentalUnit) nodeData).getParentDepartmentalUnitId() == null)
					return context.getExternalContext().getRequestContextPath()
							+ "/images/topDepart.gif";
				else
					return context.getExternalContext().getRequestContextPath()
							+ "/images/subDepart.gif";
			}
			if (nodeData instanceof SecurityUser) {
				SecurityUser su = (SecurityUser) nodeData;
				Set<Employee> es = su.getEmployee();
				Employee e = es == null ? null : es.isEmpty() ? null : es
						.iterator().next();
				if (e != null && e.getSex() != null
						&& SEX.WOMAN.equals(e.getSex()))
					return context.getExternalContext().getRequestContextPath()
							+ "/images/women.gif";
				else
					return context.getExternalContext().getRequestContextPath()
							+ "/images/man.gif";
			}
			return null;
		}

		public String getText(Object nodeData) {
			if (nodeData instanceof CentralUnit)
				return ((CentralUnit) nodeData).getName();
			if (nodeData instanceof DepartmentalUnit)
				return ((DepartmentalUnit) nodeData).getName();
			if (nodeData instanceof SecurityUser)
				return ((SecurityUser) nodeData).getFullname();
			return null;
		}

		public boolean isCascade(Object nodeData) {
			return false;
		}

		public Boolean isChecked(Object nodeData) {
			return null;
		}

		public boolean isExpanded(Object nodeData) {
			if (nodeData instanceof SecurityUser)
				return true;
			return false;
		}

		public boolean isLeaf(Object nodeData) {
			if (nodeData instanceof SecurityUser)
				return true;
			return false;
		}
	};
	@Inject
	FacesContext context;
}
